package com.controller;

import com.dao.UserDao;
import com.model.User;
import com.model.Student;
import com.model.Teacher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.SQLException;
import com.google.gson.Gson;
import java.util.HashMap;
import java.util.Map;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    private UserDao userDao = new UserDao();
    private Gson gson = new Gson();
    
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) 
            throws ServletException, IOException {
        req.getRequestDispatcher("/login.jsp").forward(req, resp);
    }
    
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) 
            throws ServletException, IOException {
        resp.setContentType("application/json;charset=UTF-8");
        
        String userType = req.getParameter("userType");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        
        // 打印接收到的参数
        System.out.println("=== 登录请求参数 ===");
        System.out.println("userType: " + userType);
        System.out.println("username: " + username);
        System.out.println("password: " + password);
        
        // 添加参数验证
        if (username == null || password == null || userType == null) {
            System.out.println("参数验证失败：参数不完整");
            sendError(resp, "请填写完整的登录信息");
            return;
        }
        
        try {
            User user = userDao.login(userType, username, password);
            System.out.println("=== 登录结果 ===");
            System.out.println("user: " + (user != null ? "登录成功" : "登录失败"));
            
            if (user != null) {
                HttpSession session = req.getSession();
                // 根据用户类型设置正确的对象
                if ("student".equals(userType)) {
                    Student student = new Student();
                    student.setSno(user.getId());
                    student.setSname(user.getName());
                    session.setAttribute("user", student);
                } else if ("teacher".equals(userType)) {
                    Teacher teacher = new Teacher();
                    teacher.setTno(user.getId());
                    teacher.setTname(user.getName());
                    // 确保设置了所有必要的教师信息
                    System.out.println("设置教师信息 - ID: " + teacher.getTno() + ", 名称: " + teacher.getName());
                    session.setAttribute("user", teacher);
                } else {
                    session.setAttribute("user", user);
                }
                session.setAttribute("userType", userType);
                
                Map<String, Object> result = new HashMap<>();
                result.put("code", 0);
                result.put("msg", "登录成功");
                resp.getWriter().write(gson.toJson(result));
            } else {
                sendError(resp, "用户名或密码错误");
            }
        } catch (SQLException e) {
            System.out.println("=== 数据库错误 ===");
            e.printStackTrace();
            sendError(resp, "系统错误，请稍后重试");
        }
    }
    
    private void sendError(HttpServletResponse resp, String message) throws IOException {
        Map<String, Object> result = new HashMap<>();
        result.put("code", 1);
        result.put("msg", message);
        resp.getWriter().write(gson.toJson(result));
    }
} 